import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import directive from './directive'
Vue.use(directive)

import jQuery from 'jquery'
import 'ztree'
import 'ztree/css/zTreeStyle/zTreeStyle.css'


import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)

Vue.config.productionTip = false

import { generateRoutes } from "@/util/util.js";



router.beforeEach(async (to, from, next) => {
  if (to.path == "/Login") {
    next();
    return;
  }
  let hasRoute = sessionStorage.getItem("hasRoute");

  if (!hasRoute && store.getters.userPermissionsTree) {

    let dynamicRoutes = generateRoutes(store.getters.userPermissionsTree);

    for (let i = 0; i < dynamicRoutes.length; i++) {
      router.addRoute("Home", dynamicRoutes[i]);
    }
    sessionStorage.setItem("hasRoute", true);
    next({ path: to.path });
  } else {
    next();
  }

})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
